package com.hytc.mymall.pop;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.hytc.mymall.R;
import com.hytc.mymall.adapter.LoginStyleAdapter;
import com.hytc.mymall.utils.BaseTools;
import com.hytc.mymall.view.LineGridView;
import java.util.List;

/**
 * Created by Administrator on 2016/12/1.
 */

public class LoginStylePop {
    private Activity activity;
    private LoginStyleAdapter adapter;
    private InputMethodManager imm;
    private ImageView iv;
    private List<String> list;
    private PopupWindow pop;
    private int selectIndex;
    private TextView tv;

    public LoginStylePop(final Activity activity, final List<String> list, final ImageView iv, final TextView tv, final SelectPopCallback selectPopCallback) {
        this.selectIndex = 0;
        this.activity = activity;
        this.iv = iv;
        this.tv = tv;
        this.list = list;
        final View inflate = LayoutInflater.from((Context)activity).inflate(R.layout.layout_pop_loginstyle, (ViewGroup)null);
        this.imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        (this.pop = new PopupWindow(inflate, BaseTools.getWindowsWidth(activity), BaseTools.dip2px((Context)activity, 50 * (int)Math.ceil(list.size() / 3.0)), true)).setOnDismissListener((PopupWindow.OnDismissListener)new PopupWindow.OnDismissListener() {
            public void onDismiss() {
                iv.startAnimation(AnimationUtils.loadAnimation((Context)activity, R.anim.title_rotating_back));
            }
        });
        final LineGridView lineGridView = (LineGridView)inflate.findViewById(R.id.lgv_pop_loginstyle);
        lineGridView.setFocusable(true);
        lineGridView.setAdapter((ListAdapter)(this.adapter = new LoginStyleAdapter((Context)activity, list)));
        lineGridView.setOnItemClickListener((AdapterView.OnItemClickListener)new AdapterView.OnItemClickListener() {
            public void onItemClick(final AdapterView<?> adapterView, final View view, final int selectIndex, final long n) {
                LoginStylePop.access$0(LoginStylePop.this, selectIndex);
                LoginStylePop.this.adapter.setSelectIndex(selectIndex);
                LoginStylePop.this.adapter.notifyDataSetChanged();
                LoginStylePop.this.pop.dismiss();
            }
        });
        this.pop.setOnDismissListener((PopupWindow.OnDismissListener)new PopupWindow.OnDismissListener() {
            public void onDismiss() {
                iv.startAnimation(AnimationUtils.loadAnimation((Context)activity, R.anim.title_rotating_back));
                tv.setText((CharSequence)list.get(LoginStylePop.this.selectIndex));
                selectPopCallback.onItemClick(LoginStylePop.this.selectIndex);
            }
        });
    }

    static /* synthetic */ void access$0(final LoginStylePop loginStylePop, final int selectIndex) {
        loginStylePop.selectIndex = selectIndex;
    }

    public void Show(final Handler handler, final int n, final EditText softKeyboardShow) {
        this.setSoftKeyboardShow(softKeyboardShow);
        new Thread() {
            @Override
            public void run() {
                final Message obtain = Message.obtain();
                obtain.what = n;
                handler.sendMessageDelayed(obtain, 100L);
            }
        }.start();
    }

    public void setSoftKeyboardShow(final EditText editText) {
        if (this.imm.isActive()) {
            this.imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
        }
    }

    public void setselection(final int n) {
        this.tv.setText((CharSequence)this.list.get(n));
    }

    public void showPop() {
        this.iv.startAnimation(AnimationUtils.loadAnimation((Context)this.activity, R.anim.title_rotating));
        this.pop.setBackgroundDrawable((Drawable)new ColorDrawable(0));
        this.pop.showAsDropDown((View)this.tv, 0, 0);
        this.pop.setAnimationStyle(R.style.popwin_anim_style);
        this.pop.setFocusable(true);
        this.pop.setOutsideTouchable(false);
        this.pop.setInputMethodMode(1);
        this.pop.setSoftInputMode(16);
        this.pop.update();
    }
}
